package com.example.myapplication;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.utils.SpUtils;
import com.example.myapplication.viewmodel.MyViewModel;

import java.util.ArrayList;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity {
    private ArrayList<String> mListOperator;
    private NavController controller;
    private Dialog mDialog;
    //    private QuestionFragment questFrag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        controller = Navigation.findNavController(this, R.id.fragment);
        NavigationUI.setupActionBarWithNavController(this, controller);
        getSupportActionBar().setHomeButtonEnabled(true);
        AppCompatCheckBox cb_add = findViewById(R.id.cb_add);
        AppCompatCheckBox cb_jianfa = findViewById(R.id.cb_jianfa);
        AppCompatCheckBox cb_chengfa = findViewById(R.id.cb_chengfa);
        AppCompatCheckBox cb_division = findViewById(R.id.cb_division);
        AppCompatCheckBox cb_time_count = findViewById(R.id.cb_time_count);
        final EditText et_question_num = findViewById(R.id.et_question_num);
        TextView tv_submit = findViewById(R.id.tv_submit);
        et_question_num.setHint("" + SpUtils.getInt(MainActivity.this, MyConstant.QUESTION_NUM, 10));
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String num = et_question_num.getText().toString();
                if (!TextUtils.isEmpty(num)) {
                    int i = Integer.parseInt(num);
                    SpUtils.putInt(MainActivity.this, MyConstant.QUESTION_NUM, i);
                    Toast.makeText(MainActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
        ImageView ivHead = findViewById(R.id.iv_head);
        Glide.with(this).load(R.mipmap.dudu_head).circleCrop().into(ivHead);
        ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View ivGroup = LayoutInflater.from(MainActivity.this).inflate(R.layout.head_window, null, false);
                ImageView iv = ivGroup.findViewById(R.id.iv_head);
                iv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mDialog.dismiss();
                    }
                });
//                View ivGroup = View.inflate(MainActivity.this, R.layout.head_window, null);
                if (mDialog == null) {
                    mDialog = new Dialog(MainActivity.this,R.style.ActionSheetDialogAnimation);
                    mDialog.setCanceledOnTouchOutside(true);
                    Window window = mDialog.getWindow();
                    window.setGravity(Gravity.CENTER);
                    mDialog.setContentView(ivGroup);
                }
                mDialog.show();

            }
        });

        mListOperator = new ArrayList<>();

        SharedPreferences sp = getApplication().getSharedPreferences(MyConstant.SAVE_SP_DATE_NAME, Context.MODE_PRIVATE);
        if (!TextUtils.isEmpty(sp.getString(MyConstant.OPERATOR_ADD, ""))) {
            mListOperator.add(sp.getString(MyConstant.OPERATOR_ADD, ""));
            cb_add.setChecked(true);

        }
        if (!TextUtils.isEmpty(sp.getString(MyConstant.OPERATOR_MULTIPLICATION, ""))) {
            mListOperator.add(sp.getString(MyConstant.OPERATOR_MULTIPLICATION, ""));
            cb_chengfa.setChecked(true);


        }
        if (!TextUtils.isEmpty(sp.getString(MyConstant.OPERATOR_DIVISION, ""))) {
            mListOperator.add(sp.getString(MyConstant.OPERATOR_DIVISION, ""));
            cb_division.setChecked(true);

        }
        if (!TextUtils.isEmpty(sp.getString(MyConstant.OPERATOR_SUBTRACTION, ""))) {
            mListOperator.add(sp.getString(MyConstant.OPERATOR_SUBTRACTION, ""));
            cb_jianfa.setChecked(true);
        }
        if (sp.getBoolean(MyConstant.TIME_COUNT, true)) {
            cb_time_count.setChecked(true);
        }


        final MyViewModel myViewModel = ViewModelProviders.of(this, new SavedStateViewModelFactory(getApplication(), this)).get(MyViewModel.class);


        myViewModel.getOperatorList().setValue(mListOperator);

        CompoundButton.OnCheckedChangeListener checkListener = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                ArrayList<String> value = myViewModel.getOperatorList().getValue();
                switch (buttonView.getId()) {
                    case R.id.cb_add:
                        //加法
                        if (isChecked) {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_ADD, "+");
                            if (!value.contains("+")) {
                                value.add("+");
                            }
                        } else {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_ADD, "");
                            if (value.contains("+")) {
                                value.remove("+");
                            }
                        }
                        break;
                    case R.id.cb_jianfa:
                        //减法
                        if (isChecked) {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_SUBTRACTION, "-");
                            if (!value.contains("-")) {
                                value.add("-");
                            }
                        } else {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_SUBTRACTION, "");
                            if (value.contains("-")) {
                                value.remove("-");
                            }
                        }
                        break;
                    case R.id.cb_chengfa:
                        //乘法
                        if (isChecked) {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_MULTIPLICATION, "x");
                            if (!value.contains("x")) {
                                value.add("x");
                            }
                        } else {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_MULTIPLICATION, "");
                            if (value.contains("x")) {
                                value.remove("x");
                            }
                        }
                        break;
                    case R.id.cb_division:
                        //除法
                        if (isChecked) {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_DIVISION, "÷");
                            if (!value.contains("÷")) {
                                value.add("÷");
                            }
                        } else {
                            SpUtils.putString(MainActivity.this, MyConstant.OPERATOR_DIVISION, "");
                            if (value.contains("÷")) {
                                value.remove("÷");
                            }
                        }
                        break;
                    case R.id.cb_time_count:
                        //使用计时功能
                        SpUtils.putBoolean(MainActivity.this, MyConstant.TIME_COUNT, isChecked);
                        break;
                }
                myViewModel.getOperatorList().setValue(value);
            }
        };
        cb_add.setOnCheckedChangeListener(checkListener);
        cb_jianfa.setOnCheckedChangeListener(checkListener);
        cb_chengfa.setOnCheckedChangeListener(checkListener);
        cb_division.setOnCheckedChangeListener(checkListener);
        cb_time_count.setOnCheckedChangeListener(checkListener);


//        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
//            @Override
//            public void onDrawerOpened(View drawerView) {
//                super.onDrawerOpened(drawerView);
//            }
//            @Override
//            public void onDrawerClosed(View drawerView) {
//                super.onDrawerClosed(drawerView);
//            }
//        };
//        mDrawerLayout.addDrawerListener(mDrawerToggle);

    }

    @Override
    public boolean onSupportNavigateUp() {
        if (controller.getCurrentDestination().getId() == R.id.questionFragment) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("确定要退出吗？");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    controller.navigateUp();
                }
            });
            builder.setNegativeButton("取消", null);
            AlertDialog dialog = builder.create();
            dialog.show();
        } else if (controller.getCurrentDestination().getId() == R.id.titleFragment) {
            finish();
        } else {
            controller.navigate(R.id.titleFragment);
        }
        return super.onNavigateUp();
    }

    @Override
    public void onBackPressed() {
        onSupportNavigateUp();
    }


}
